import 'package:flutter/material.dart';
import 'package:flutterapp2/extension/extension.dart';

class TestAnim2Page extends StatefulWidget {
  @override
  _TestAnim2PageState createState() => _TestAnim2PageState();
}

class _TestAnim2PageState extends State<TestAnim2Page> with SingleTickerProviderStateMixin {
  ScrollController _scrollController;
  AnimationController _animationController;
  Animation<double> _animation;

  @override
  void initState() {
    super.initState();
    _animationController = AnimationController(
      duration: Duration(milliseconds: 200),
      vsync: this,
    );
    _animation = Tween(begin: -20.0, end: 20.0).animate(_animationController)
      ..addListener(() {
        setState(() {});
      });

    _scrollController = ScrollController();
    double lastOffset = 0;
    _scrollController.addListener(() {
      // var direction = _scrollController.position.axisDirection;
      if (_scrollController.offset > lastOffset) {
        // 向下滑动
        _animationController.forward();
      } else {
        _animationController.reverse();
      }
      lastOffset = _scrollController.offset;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("test_anim"),
      ),
      body: Stack(
        overflow: Overflow.visible,
        children: <Widget>[
          SingleChildScrollView(
            controller: _scrollController,
            child: Column(
              children: <Widget>[
                Container(
                  height: 500,
                ),
                Container(
                  color: Colors.yellow,
                  height: 1000,
                ),
                Row()
              ],
            ),
          ),
          Positioned(
            right: _animation.value,
            top: 300,
            child: CircleAvatar(
              child: Text("活动"),
              radius: 20,
              backgroundColor: Colors.blueAccent,
            ),
          ),
        ],
      ),
    );
  }
}
